<template>
	<view class="container">

		<button type="primary" @click="login">登录</button>

	</view>
</template>

<script>
	import {
		Base64
	} from 'js-base64'

	export default {
		data() {
			return {

			};
		},
		methods: {

			login: function() {
				let _this = this;

				var users = [{
						Account: "admin",
						Password: "123456"
					},
					{
						Account: "13953219992",
						Password: "ffffff"
					},
					{
						Account: "15762274612",
						Password: "132"
					},
					{
						Account: "15153286155",
						Password: "123456"
					},
					{
						Account: "1001",
						Password: "123456"
					},
					{
						Account: "1002",
						Password: "123456"
					},
					{
						Account: "1003",
						Password: "123456"
					},
					{
						Account: "1004",
						Password: "123456"
					},
					{
						Account: "1005",
						Password: "123456"
					},
					{
						Account: "1006",
						Password: "123456"
					},
					{
						Account: "1007",
						Password: "123456"
					},
					{
						Account: "1008",
						Password: "123456"
					},
					{
						Account: "1009",
						Password: "123456"
					},
				];

				var tokens = []

				users.forEach((item,index) => {
					
					//正式代码
					uni.request({
						url: _this.sysInfo.BasePath + 'Auth/Login',
						method: 'POST',
						data: item,
						header: {
							'content-type': 'application/json'
						},
						success: (res) => {
							if (res && res.data && res.data.Succeeded === true) {
								var user = res.data.Data;
								var userJsonStr = JSON.stringify(user);
								var token = Base64.encode(`${userJsonStr}zhinian`)

								tokens.push(token)
							} else {
								_this.sysUtils.tip({
									sender: _this,

									title: '登录失败！',
									icon: 'error'
								});
							}
						},
						fail: () => {
							_this.sysUtils.tip({
								sender: _this,
								title: '登录失败！',
								icon: 'error'
							});
						},
						complete: () => {}
					});
				})

				setTimeout(() => {
					tokens.forEach((item, index) => {
						if(index==3){
							_this.cancel("1F2868E3-F8CC-476D-964A-AFC6E52CF363",item)
						}
						_this.qiangGou({
							"Id": "4e1b72bc-a7f3-43d1-ae0f-3b0c0965846c"
						}, item, index)
					})
				}, 3000)

			},
			qiangGou(item, authToken, index) {
				let _this = this;
				console.log('开始' + index)
				//正式代码
				uni.request({
					url: _this.sysInfo.BasePath + 'RushPurchase/BuyProduct',
					method: 'Post',
					data: item,
					header: {
						'content-type': 'application/json',
						Authorization: `${authToken}`
					},
					success: (res) => {
						if (res && res.data && res.data.Succeeded === true) {
							console.log('抢购产品成功！');
						} else {
							console.log('抢购产品失败！');
						}
					},
					fail: () => {},
					complete: () => {
						console.log('结束' + index)
					}
				});

			},
			cancel: function(id, authToken) {
				let _this = this;
				var queryData = {
					State: 5,
					Id: id
				};
				uni.request({
					url: _this.sysInfo.BasePath + 'RushPurchase/ChangeOrderState',
					method: 'POST',
					data: queryData,
					header: {
						'content-type': 'application/json',
						Authorization: `${authToken}`
					},
					success: (res) => {
						if (res && res.data && res.data.Succeeded === true) {
							console.log('更改订单状态成功！');

						} else {
							console.log('更改订单状态失败！');
						}
					},
					fail: () => {
						console.log('更改订单状态失败！');
					},
					complete: () => {}
				});
			}

		},
		created() {
			let _this = this;
		}
	};
</script>

<style lang="scss">



</style>